home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / image / colorm~1.jav next >
Encoding:
Text File  |  1996-01-12  |  3.4 KB  |  112 lines

  1. /*
  2.  * @(#)ColorModel.java    1.9 95/12/14 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. /**
  23.  * A class that encapsulates the methods for translating from pixel values
  24.  * to alpha, red, green, and blue color components for an image.  This
  25.  * class is abstract.
  26.  *
  27.  * @see IndexColorModel
  28.  * @see DirectColorModel
  29.  *
  30.  * @version    1.9 12/14/95
  31.  * @author     Jim Graham
  32.  */
  33. public abstract class ColorModel {
  34.     protected int pixel_bits;
  35.  
  36.     private static ColorModel RGBdefault;
  37.  
  38.     /**
  39.      * Return a ColorModel which describes the default format for
  40.      * integer RGB values used throughout the AWT image interfaces.
  41.      * The format for the RGB values is an integer with 8 bits
  42.      * each of alpha, red, green, and blue color components ordered
  43.      * correspondingly from the most significant byte to the least
  44.      * significant byte, as in:  0xAARRGGBB
  45.      */
  46.     public static ColorModel getRGBdefault() {
  47.     if (RGBdefault == null) {
  48.         RGBdefault = new DirectColorModel(32,
  49.                           0x00ff0000,    // Red
  50.                           0x0000ff00,    // Green
  51.                           0x000000ff,    // Blue
  52.                           0xff000000    // Alpha
  53.                           );
  54.     }
  55.     return RGBdefault;
  56.     }
  57.  
  58.     /**
  59.      * Constructs a ColorModel which describes a pixel of the specified
  60.      * number of bits.
  61.      */
  62.     public ColorModel(int bits) {
  63.     pixel_bits = bits;
  64.     }
  65.  
  66.     /**
  67.      * Returns the number of bits per pixel described by this ColorModel.
  68.      */
  69.     public int getPixelSize() {
  70.     return pixel_bits;
  71.     }
  72.  
  73.     /**
  74.      * The subclass must provide a function which provides the red
  75.      * color compoment for the specified pixel.
  76.      * @return        The red color component ranging from 0 to 255
  77.      */
  78.     public abstract int getRed(int pixel);
  79.  
  80.     /**
  81.      * The subclass must provide a function which provides the green
  82.      * color compoment for the specified pixel.
  83.      * @return        The green color component ranging from 0 to 255
  84.      */
  85.     public abstract int getGreen(int pixel);
  86.  
  87.     /**
  88.      * The subclass must provide a function which provides the blue
  89.      * color compoment for the specified pixel.
  90.      * @return        The blue color component ranging from 0 to 255
  91.      */
  92.     public abstract int getBlue(int pixel);
  93.  
  94.     /**
  95.      * The subclass must provide a function which provides the alpha
  96.      * color compoment for the specified pixel.
  97.      * @return        The alpha transparency value ranging from 0 to 255
  98.      */
  99.     public abstract int getAlpha(int pixel);
  100.  
  101.     /**
  102.      * Returns the color of the pixel in the default RGB color model.
  103.      * @see ColorModel#getRGBdefault
  104.      */
  105.     public int getRGB(int pixel) {
  106.     return (getAlpha(pixel) << 24)
  107.         | (getRed(pixel) << 16)
  108.         | (getGreen(pixel) << 8)
  109.         | (getBlue(pixel) << 0);
  110.     }
  111. }
  112.